<?php

namespace App\Console\Commands;

use App\Models\Users;
use App\Models\UserSubscribe;
use App\Utils\MiniProgramUtil;
use Illuminate\Console\Command;

class SendTemplate extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'send-template';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $today = date('Y-m-d');
        $mini = new MiniProgramUtil();
        Users::where('auth_subscribe', 0)->chunk(100, function ($users) use ($today, $mini){
            /** @var Users $user */
            foreach($users as $user){
                /** @var UserSubscribe $subscribe */
                $subscribe = UserSubscribe::where([
                    'user_id' => $user->id,
                    'status' => 2,
                    'is_use' => 0,
                ])->orderBy('id', 'desc')->first();
                if (!empty($subscribe)){
                    $subscribe->is_use = 1;
                    $subscribe->save();

                    $mini->studyNotice($user, explode(',', $subscribe->template_id)[0]);

                }


            }

            unset($users);
        });
    }
}
